/**
 * @filename:TransferController Dec 15, 2023
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;

import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.domain.Transfer;
import com.wonder.app.service.TransferService;
import com.wonder.app.request.TransferRequest;
import com.wonder.framework.authorization.CheckAdminToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

import javax.servlet.http.HttpServletRequest;
import javax.swing.plaf.basic.BasicIconFactory;

/**
 * 
 * <p>说明： 网页调拨API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    Dec 15, 2023
 *
 */
@Api(tags = "网页调拨")
@RestController
@RequestMapping("/app/Transfer")
public class TransferController {
	
	@Autowired
	private TransferService transferService;

	@Autowired
	private CheckAdminToken checkAdminToken;

	@Autowired
	private SysAdminService adminService;
	
	@ApiOperation(value = "网页调拨列表")
	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public Map<String,Object> index(@RequestBody TransferRequest transferRequest) {
		Page<Transfer> page = new Page<Transfer>(transferRequest.getPage(), transferRequest.getLimit());
		IPage<Transfer> list = transferService.selectList(transferRequest, page);
		
		Map<String,Object> retMap=new HashMap<>();
		retMap.put("data",CommonPage.restPage(list));
		
  
		return AjaxMap.success(retMap);
	}
	
	/**
	 * @explain 修改排序开关
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/updateExt")
	@ApiOperation(value = "修改排序开关", notes = "作者：Wonder")
	public AjaxResult<Transfer> updateExt(@RequestBody Transfer entity){
		if (null!=entity) {
			boolean rsg = transferService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 添加
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/add")
	@ApiOperation(value = "添加", notes = "作者：Wonder")
	public AjaxResult<Transfer> add(@RequestBody Transfer entity){

		if (null!=entity) {
		    
		    String validate=transferService.validate(entity,"new");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);    
			boolean rsg = transferService.save(entity);
			if (rsg) {
				  return AjaxResult.success("system.insertSuccess");
			  }else {
				  return AjaxResult.failed("system.insertFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 查询对象  <swagger GET请求>
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/getUpdateInfo")
	@ApiOperation(value = "获取网页调拨对象", notes = "作者：Wonder")
	public AjaxResult<Transfer> getUpdateInfo(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");
		Long id=Long.parseLong(map.get("id").toString());		
		 Transfer transfer=transferService.getById(id);
		
		if (null!=transfer ) {
			 return AjaxResult.success(transfer);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}
	
	/**
	 * @explain 修改
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "作者：Wonder")
	public AjaxResult<Transfer> update(@RequestBody Transfer entity){
		if (null!=entity) {
		    String validate=transferService.validate(entity,"update");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);
			boolean rsg = transferService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 删除
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除", notes = "作者：Wonder")
	public AjaxResult<Boolean> delete(@RequestBody Map<String,Object> map){
		if(map==null)return AjaxResult.failed("system.parameterError");
		String billNo = map.get("billNo").toString();

	    boolean rsg = transferService.removeTransfer(billNo);
	    if (rsg) {
		    return AjaxResult.success(rsg);
	    }else {
		   return AjaxResult.failed("system.deleteFailed");
	    }
		
	}
	
	/**
	 * @explain 查看调拨单详情
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/detail")
	@ApiOperation(value = "获取网页调拨对象明细", notes = "作者：Wonder")
	public AjaxResult<VtuzxMap> detail(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");

	    VtuzxMap data = transferService.getDetail(map);

		if (null!=data ) {
			 return AjaxResult.success(data);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}

	/**
	 * 根据物料和批次查询即时库存物料信息
	 * @param map
	 * @return
	 */
	@PostMapping("/get_material_from_stock")
	public Map<String,Object> getMaterialFromStock(@RequestBody Map<String,Object> map){

		Map<String,Object> data = transferService.getMaterialFromStock(map);
		return AjaxMap.success(data);
	}


	/**
	 * 修改调拨单
	 * @param map
	 * @return
	 */
	@PostMapping("/update_transfer")
	public AjaxResult<VtuzxMap> updateTransfer(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		String userName = user.getName();
		Long user_id = user.getUser_id();
		VtuzxMap transfer = transferService.updateTransfer(userName, user_id, map);
		return AjaxResult.success(transfer);
	}


	/**
	 * 反审调拨单
	 * @param map
	 * @return
	 */
	@PostMapping("/unAudit")
	@ApiOperation(value = "反审核其他入库单", notes = "作者：sunyf")
	public AjaxResult<Boolean> unAudit(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		String userName = user.getName();
		Long user_id = user.getUser_id();
		if(map==null) return AjaxResult.failed("system.parameterError");
		String billNo = map.get("billNo").toString();
		boolean b = transferService.unAuditTransfer(userName, user_id, billNo);
		return AjaxResult.success(b);
	}

	/**
	 * 审核调拨单
	 * @return
	 */
	@PostMapping("/audit")
	@ApiOperation(value = "审核采购入库单", notes = "作者：sunyf")
	public AjaxResult<Boolean> audit(HttpServletRequest request,@RequestBody Map<String,Object> map)throws Exception{
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		String userName = user.getName();
		Long user_id = user.getUser_id();
		if(map==null) return AjaxResult.failed("system.parameterError");
		String billNo = map.get("billNo").toString();
		boolean b = transferService.auditTransfer(userName, user_id, billNo);
		return AjaxResult.success(b);
	}

	/**
	 * 创建调拨单(中台)
	 * @param map
	 * @return
	 */
	@PostMapping("/create_transfer_web")
	public AjaxResult<VtuzxMap> createTransferWeb(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		String userName = user.getName();
		Long user_id = user.getUser_id();
		VtuzxMap transfer = transferService.createTransferWeb(userName, user_id, map);
		return AjaxResult.success(transfer);
	}
}